<template>
  <div class="goods">
    <!-- 目录导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query">
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button size="medium" type="primary" icon="el-icon-plus">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格主体区域 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="价格￥/件"></el-table-column>
        <el-table-column prop="goods_number" label="数量(件)"></el-table-column>
        <el-table-column prop="goods_weight" label="重量(kg)"></el-table-column>
        <el-table-column prop="goods_state" label="状态">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.goods_state == '2'">已审核</el-tag>
            <el-tag type="info" v-else-if="scope.row.goods_state == '1'">审核中</el-tag>
            <el-tag type="danger" v-else>未通过</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="hot_mumber" label="热销量(件)"></el-table-column>
        <el-table-column prop="is_promote" label="是否热销">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.is_promote === true">是</el-tag>
            <el-tag v-else>否</el-tag>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="goodsTotal">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 商品总数
      goodsTotal: 0,
      // 商品列表
      goodsList: [],
      // 商品数据列表查询参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 2
      },
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 获取商品列表
    async getGoodsList() {
      const {data: res} = await this.$axios.get('goods', {params: this.queryInfo})
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      console.log(res);
      this.goodsList = res.data.goods
      this.goodsTotal = res.data.total
    },
    // 监听pagesize的改变事件
    handleSizeChange(newSize) {
      console.log(`每页 ${newSize} 条`)
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 监听页码值的改变
    handleCurrentChange(newPage) {
      console.log(`当前页: ${newPage}`)
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
  },
}
</script>

<style lang="less" scoped>

</style>